from rest_framework import serializers
from .models import FileDiffDefaultField

'''
    获取 gitlab 文件接口时的字段校验
'''
# serializers.py


class GetGitlabFileBodySerializer(serializers.ModelSerializer):
    src = serializers.JSONField()
    srcPath = serializers.JSONField()
    dest = serializers.JSONField()
    destPath = serializers.JSONField()

    class Meta:
        model = FileDiffDefaultField
        fields = ['src', 'srcPath', 'dest', 'destPath']

    def validate_src(self, value):
        if not isinstance(value, list) or len(value) != 3:
            raise serializers.ValidationError("src must be a list of three elements.")
        if not isinstance(value[0], int):
            raise serializers.ValidationError("First element of src must be an integer.")
        if not isinstance(value[1], str):
            raise serializers.ValidationError("Second element of src must be a string.")
        if not isinstance(value[2], str):
            raise serializers.ValidationError("Third element of src must be a string.")
        return value

    def validate_srcPath(self, value):
        if not isinstance(value, list) or len(value) != 1:
            raise serializers.ValidationError("src must be a list of three elements.")
        if not isinstance(value[0], str):
            raise serializers.ValidationError("First element of src must be an integer.")
        return value

    def validate_dest(self, value):
        if not isinstance(value, list) or len(value) != 3:
            raise serializers.ValidationError("dest must be a list of three elements.")
        if not isinstance(value[0], int):
            raise serializers.ValidationError("First element of dest must be an integer.")
        if not isinstance(value[1], str):
            raise serializers.ValidationError("Second element of dest must be a string.")
        if not isinstance(value[2], str):
            raise serializers.ValidationError("Third element of dest must be a string.")
        return value

    def validate_destPath(self, value):
        if not isinstance(value, list) or len(value) != 1:
            raise serializers.ValidationError("src must be a list of three elements.")
        if not isinstance(value[0], str):
            raise serializers.ValidationError("First element of src must be an integer.")
        return value